package com.chance.cc.crawler.development.command.meta.keyword.autohome;

import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lt
 * @version 1.0
 * @date 2020-12-29 14:09:55
 * @email okprog@sina.com
 */
public class AutoHomeKeywords {
    private static final String domain = "autohome";
    private static CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
            new CrawlerDomainKeywordCommand("192.168.1.217",9599);

    /**
     * 实时采集车系关键词
     */
    @Test
    public void  series(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "series";
        String filepath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\series.txt";
        List<String> content = getContent(filepath);
        for (String keyword : content) {
//            keyword = keyword.split("cn/")[1].replace("/","");
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }


    /**
     * 临时采集关键词 临时增加关键词
     */
    @Test
    public void  adhocTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "adhocKw";
        String filePath = "E:\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\adhoc-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
            //keyword=keyword.split("cn/")[1].split("/")[0];
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
           // crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    /**
     * 吉利项目项目车系关键词
     */
    @Test
    public void  geelySeries(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "geely";
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\jeely-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    /**
     * 宝马项目关键词 一个月更换一次
     */
    @Test
    public void bmwTest(){
        String site = "bmwKw";
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String filePath = "E:\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\adhoc-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
          // keyword = keyword.split("cn/")[1].replace("/","");
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
        //   crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
           // crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    /**
     * 通用项目关键词 一个月更换一次
     */
    @Test
    public void  sgmTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "sgmKw";
        String filePath = "E:\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\sgmkw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }


    /**
     * 经销商实时采集城市、品牌关键词
     * {"北京":"保时捷","沃尔沃"}
     */
    @Test
    public void  dealer(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "dealer";
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\autohome\\dealer-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }



    public static List<String> getContent(String filepath)  {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  lineLists;
    }
}
